﻿using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Models;
using DFGK_WMS.WebAPI.AttributeFile;
using DFGK_WMS.WebAPI.Controllers.Result;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NetTaste;
using System.Security.Claims;
using System.Xml.Linq;

namespace DFGK_WMS.WebAPI.Controllers.基础数据
{
    /// <summary>
    /// 库区
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [HttpIdempotent(WaitMillisecond = 10000, CacheMillisecond = 3000)]
    public class AreaController : ControllerBase
    {
        private readonly ILogRepository logRepository;
        public AreaController(IAreaRepository areaService, ILogRepository logRepository)
        {
            RreaService = areaService;
            this.logRepository = logRepository;
        }
        public IAreaRepository RreaService { get; set; }
        /// <summary>
        /// 添加库区
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddArea(AreaDto obj)
        {
            string message = "";
            if (string.IsNullOrEmpty(obj.Code)||obj.Code.Length<=0)
            {
                return Ok(BaseResult.Result("400", "编码不能为空！"));
            }
            ApiResult apiresult = new ApiResult();
            string uname = HttpContext.User.FindFirstValue("UserName");
            string runame = HttpContext.User.FindFirstValue("RealName");
            int uid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
            obj.CreateUserId = uid;
            obj.CreateUserName = uname;
            bool IsOK = RreaService.AddArea(obj,out message);
            if (IsOK)
            {               
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 4;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = uid;
                logg.Description = uname + "创建了库区" + obj.Name;
                //获取前端IP地址
                logg.Ipaddress = ip;

                logRepository.AddTLod(logg);
                apiresult.code = "200";
                apiresult.message = "添加成功!";
                apiresult.data = true;
                return Ok(apiresult);
            }
            apiresult.code = "500";
            apiresult.message = "添加失败！"+ message;
            apiresult.data = false;
            return Ok(apiresult);
        }
        /// <summary>
        /// 显示库区信息（弃用）
        /// </summary>
        /// <param name="kcode"></param>
        /// <param name="kname"></param>
        /// <param name="kid"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetAreaList(string? kname = "", int kid = 0, int pageIndex = 1, int pageSize = 3)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            ApiResult apiresult = new ApiResult();
            int totalCount;
            var list = RreaService.GetAreaList(kname, kid, pageIndex, pageSize, out totalCount);
            if (list != null)
            {
                apiresult.code = "200";
                apiresult.message = "获取成功!";
                apiresult.data = list;
                apiresult.count = totalCount;
                return Ok(apiresult);
            }
            apiresult.code = "500";
            apiresult.message = "获取失败!";
            apiresult.data = true;
            return Ok(apiresult);
        }
        /// <summary>
        /// 显示库区信息
        /// </summary>
        /// <param name="kname"></param>
        /// <param name="wid"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetAreanList(string? kname = "", int wid = 0, int pageIndex = 1, int pageSize = 3)
        {
            if(pageIndex<=0 || pageSize<=0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount;

            var list = RreaService.GetAreanList(kname, wid, pageIndex, pageSize, out totalCount);

            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }
        /// <summary>
        /// 删除库区
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelArea(int uid)
        {
            if(uid<=0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            ApiResult apiresult = new ApiResult();
            bool IsOK = RreaService.DelArea(uid);
            if (IsOK)
            {
                string uname = HttpContext.User.FindFirstValue("UserName");
                string runame = HttpContext.User.FindFirstValue("RealName");
                int rid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 5;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = rid;
                logg.Description = uname + "删除了库区";
                //获取前端IP地址
                logg.Ipaddress = ip;

                logRepository.AddTLod(logg);
                apiresult.code = "200";
                apiresult.message = "删除成功!";
                apiresult.data = true;
                return Ok(apiresult);
            }
            apiresult.code = "500";
            apiresult.message = "删除失败!";
            apiresult.data = true;
            return Ok(apiresult);
        }
        /// <summary>
        /// 批量删除库区
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelAreaList(int[] uidList)
        {
            if (uidList.Length <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            bool daRes = true;
            ApiResult apiresult = new ApiResult();
            foreach (var uid in uidList)
            {
                bool IsOK = RreaService.DelArea(uid);
                string uname = HttpContext.User.FindFirstValue("UserName");
                string runame = HttpContext.User.FindFirstValue("RealName");
                int rid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 5;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = rid;
                logg.Description = IsOK ? uname + "删除了库区" : uname + "删除库区失败";
                //获取前端IP地址
                logg.Ipaddress = ip;
                logRepository.AddTLod(logg);
                if (!IsOK)
                {
                    daRes = false;
                    break;
                }
            }
            
            if (daRes)
            {
                apiresult.code = "200";
                apiresult.message = "删除成功!";
                apiresult.data = true;
                return Ok(apiresult);
            }
            apiresult.code = "500";
            apiresult.message = "删除失败!";
            apiresult.data = true;
            return Ok(apiresult);
        }
        /// <summary>
        /// 修改库区信息
        /// </summary>
        /// <param name="u"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdateArea(AreaDto u)
        {
            if(u.Id<=0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            if (string.IsNullOrEmpty(u.Code)||u.Code.Length<=0)
            {
                return Ok(BaseResult.Result("400", "编码不能为空！"));
            }
            string message = "";
            string uname = HttpContext.User.FindFirstValue("UserName");
            string runame = HttpContext.User.FindFirstValue("RealName");
            int uid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
            u.ModifyUserId = uid;
            u.ModifyUserName = runame;
            var res = RreaService.UpdateArea(u,out message);
            ApiResult apiresult = new ApiResult();
            if (res)
            {        
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 6;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = uid;
                logg.Description = uname + "修改了库区" + u.Name;
                //获取前端IP地址
                logg.Ipaddress = ip;

                logRepository.AddTLod(logg);
                apiresult.code = "200";
                apiresult.message = "修改成功!";
                apiresult.data = true;
                return Ok(apiresult);
            }
            apiresult.code = "500";
            apiresult.message = "修改失败！"+message;
            apiresult.data = true;
            return Ok(apiresult);
        }
        /// <summary>
        /// 根据id获取信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetAreaById(int id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var res = RreaService.GetAreaById(id);
            ApiResult apiresult = new ApiResult();
            if (res != null)
            {
                apiresult.code = "200";
                apiresult.message = "获取成功!";
                apiresult.data = res;
                return Ok(apiresult);
            }
            apiresult.code = "500";
            apiresult.message = "获取失败!";
            apiresult.data = true;
            return Ok(apiresult);
        }
        /// <summary>
        /// 获取库房(仓库)信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetTwarehouseList()
        {
            var res = RreaService.GetTwarehouseList();
            ApiResult apiresult = new ApiResult();
            if (res != null)
            {
                apiresult.code = "200";
                apiresult.message = "获取成功!";
                apiresult.data = res;
                return Ok(apiresult);
            }
            apiresult.code = "500";
            apiresult.message = "获取失败!";
            apiresult.data = true;
            return Ok(apiresult);
        }

        /// <summary>
        /// 根据库房获取库区
        /// </summary>
        /// <param name="wid"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetWhIdAreaList(int wid)
        {
            if(wid<=0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }

            var list = RreaService.GetWhIdAreaList(wid);
            if(list!=null)
            {
                return Ok(BaseResult.Result(list, "获取成功！", "200", 0));
            }
            return Ok(BaseResult.Result(null, "获取失败", 0));
        }
    }
}
